<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="repairTime">
        <el-select v-model="queryParams.deviceId" placeholder="请选择设备名称">
          <el-option v-for="dict in deviceNames" :key="dict.deviceId" :label="dict.deviceName"
            :value="dict.deviceId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修时间" prop="repairTime">
        <el-date-picker clearable v-model="repairTime" @change="handleSearchTimeChange" type="daterange"
          range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['deviceBase:device:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['deviceBase:device:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['deviceBase:device:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['deviceBase:device:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="70" type="index" align="center" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="维修原因" align="center" prop="repairReason" />
      <el-table-column label="提出时间" align="center" prop="proposeTime" />
      <el-table-column label="维护人员" align="center" prop="repairUserName" />
      <el-table-column label="处理结果" align="center" prop="handleResult" />
      <!-- <el-table-column label="厂家" align="center" prop="factoryId">
        <template #default="scope">
          <dict-tag :options="factory" :value="scope.row.factoryId" />
        </template>
      </el-table-column> -->
      <el-table-column label="维修时间" align="center" prop="repairTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['deviceBase:device:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['deviceBase:device:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备维护对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deviceRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="设备名称" prop="deviceId">
          <el-select v-model="form.deviceId" placeholder="请选择设备名称">
            <el-option v-for="dict in deviceNames" :key="dict.deviceId" :label="dict.deviceName"
              :value="dict.deviceId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护人" prop="repairUser">
          <el-select v-model="form.repairUser" filterable placeholder="请选择维护人">
            <el-option v-for="user in userNames" :key="user.userId" :label="user.nickName"
              :value="user.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提出时间" prop="proposeTime">
          <el-date-picker clearable v-model="form.proposeTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择提出时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修时间" prop="repairTime">
          <el-date-picker clearable v-model="form.repairTime" type="datetime" value-format="YYYY-MM-DD  HH:mm:ss" placeholder="请选择维修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理结果" prop="handleResult">
          <el-input v-model="form.handleResult" placeholder="请输入处理结果" />
        </el-form-item>
        <el-form-item label="维修原因" prop="repairReason">
          <el-input v-model="form.repairReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Device">
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/deviceBase/device";
import { listUser } from "@/api/system/user.js"
import { listCard } from "@/api/deviceBase/card";
import { ref } from "vue";
const { proxy } = getCurrentInstance();
const { factory } = proxy.useDict('factory');
const deviceNames = ref([])
const userNames = ref([])
const deviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const repairTime = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceName: null,
    repairTimeStart: null,
    repairTimeEnd: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);
/** 查询卡片管理列表 */
function getDeviceNames() {
  listUser({
    pageNum: 1,
    pageSize: 1000,
  }).then(response => {
    userNames.value = response.rows;
  });
  listCard({
    pageNum: 1,
    pageSize: 1000,
  }).then(response => {
    deviceNames.value = response.rows;
  });
}
// 
const handleSearchTimeChange = (item) => {
  queryParams.value.repairTimeStart = item[0]
  queryParams.value.repairTimeEnd = item[1]
}
/** 查询设备维护列表 */
function getList() {
  loading.value = true;
  listDevice(queryParams.value).then(response => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    deviceId: null,
    repairReason: null,
    factoryId: null,
    repairTime: null,
    handleResult: null,
    proposeTime: null,
    repairUser: null,
  };
  proxy.resetForm("deviceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  repairTime.value = null
  queryParams.value.deviceId = null
  queryParams.value.repairTimeStart = null
  queryParams.value.repairTimeEnd = null
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.repairId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备维护";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _repairId = row.repairId || ids.value
  getDevice(_repairId).then(response => {
    open.value = true;
    form.value = response.data;
    title.value = "修改设备维护";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceRef"].validate(valid => {
    if (valid) {
      if (form.value.repairId != null) {
        updateDevice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDevice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _repairIds = row.repairId || ids.value;
  proxy.$modal.confirm('是否确认删除设备维护编号为"' + _repairIds + '"的数据项？').then(function () {
    return delDevice(_repairIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('deviceBase/device/export', {
    ...queryParams.value
  }, `device_${new Date().getTime()}.xlsx`)
}

getDeviceNames()
getList();
</script>
<style lang="scss" scoped>
.el-pagination {
  width: 100%;
  height: 50px;
  background-color: red;
}
</style>
